mod common;
mod service;
mod sql;

use crate::common::rdius::ChatRedis;
use crate::common::utils::sign_utils::{rsa_sign, HashType};
use crate::service::server::chat::ChatServer;
use actix::Actor;
use actix_web::{web, App, HttpServer};
use service::webrtc::webrtc::WebrtcServer;
use service::{model, routes};
use sqlx::postgres::PgPoolOptions;
use sqlx::Row;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let pool = PgPoolOptions::new()
        .max_connections(5)
        // .connect("postgres://postgres:djy@localhost:5433/postgres").await.unwrap();
        .connect("postgres://djy:djy@localhost:5432/chat")
        .await
        .unwrap();
    let redis = ChatRedis {
        message_list: HashMap::new(),
        message: HashMap::new(),
    };
    let app_state = Arc::new(AtomicUsize::new(0));

    let server = ChatServer::new(pool.clone(), redis, app_state.clone());
    let server = server.start();
    let webrtc_server = WebrtcServer::new().start();
    let mut pics: Vec<String> = Vec::new();
    pics.push("http://img.crcz.com/allimg/202002/14/1581672665926474.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/202002/14/1581672665629641.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/202002/14/1581672666263268.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/202002/14/1581672666118457.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/202002/16/1581829117462462.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/202002/16/1581829117496974.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/201809/28/1538072527267700.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/201810/03/1538572662747246.jpg".to_string());
    pics.push("http://img.crcz.com/allimg/201810/03/1538572662338746.jpg".to_string());
    pics.push("http://img.jj20.com/up/allimg/tx20/150412015814645.jpg".to_string());

    HttpServer::new(move || {
        App::new()
            .data(pool.clone())
            .data(server.clone())
            .data(pics.clone())
            .data(webrtc_server.clone())
            .service(routes::websocket::websocket)
            .service(routes::websocket::webrtc)
            .service(routes::store::goods)
            .service(routes::store::shop)
            .service(routes::store::home)
            .service(routes::store::register)
            .service(routes::store::login)
            .service(routes::store::info)
            .service(routes::store::gst)
        // .service(route::seller_message_list)
    })
    //.bind("192.168.3.95:8080")?
    .bind("0.0.0.0:80")?
    .run()
    .await
}
